package com.aliang.shopping.model.converter;

import cn.hutool.core.util.ObjectUtil;
import com.aliang.shopping.common.PageResult;
import com.aliang.shopping.model.enums.BusinessStatusType;
import com.aliang.shopping.model.enums.UserTypeEnum;
import com.baomidou.mybatisplus.core.metadata.IPage;

import java.util.Collections;
import java.util.Objects;


public class CommonConverter<T> {


    public PageResult<T> toPageResult(IPage<T> pageResult) {
        PageResult<T> result = new PageResult<>();
        if (ObjectUtil.hasEmpty(pageResult, pageResult.getRecords(), pageResult.getTotal())) {
            //查询结果为空
            result.setTotal(0L);
            result.setRows(Collections.emptyList());
        } else {
            result.setTotal(pageResult.getTotal());
            result.setRows(pageResult.getRecords());
        }
        return result;
    }

    ;

    /**
     * 通过数字获取对应的信息值
     *
     * @param code
     * @param trueMsg
     * @param falseMsg
     * @return
     */
    public static String getMsg(Integer code, String trueMsg, String falseMsg) {
        return code == 1 ? trueMsg : falseMsg;
    }

    /**
     * 通过字符串获取对应的数字
     *
     * @param source
     * @param trueMsg
     * @return
     */
    public static Integer getNum(String source, String trueMsg) {
        return Objects.equals(source, trueMsg) ? 1 : 0;
    }


    /**
     * 通过管理员类型获取管理员角色
     */
    public static Integer getAdminRoleNum(Integer typeNum) {
        return typeNum == 0 ? UserTypeEnum.ADMIN.getCode() : UserTypeEnum.SUPER_ADMIN.getCode();
    }


    /**
     * 通过商家状态获取对应信息
     */
    public static String getBusinessStatusMsg(Integer typeNum) {
        if (Objects.equals(typeNum, BusinessStatusType.NORMAL.getCode())) {
            return "正常";
        }
        if (Objects.equals(typeNum, BusinessStatusType.DISABLE.getCode())) {
            return "禁用";
        }
        if (Objects.equals(typeNum, BusinessStatusType.UNDER_REVIEW.getCode())) {
            return "审核中";
        }
        return "审核失败";

    }
}
